import { GameParams } from "../../Config/Config";

const { log } = cc;


/** 
 * 日志管理 
 */
export default class Logger {
    //打印范围内时间消耗
    //记录开始计时
    public static start(describe: string = "Time"): void {
        console.time(describe);
    }

    public static end(describe: string = "Time"): void {
        console.timeEnd(describe);
    }

    //打印表格
    public static table(msg: any) {
        if (!GameParams.Debug) {
            return;
        }
        console.table(msg);
    }

    /**
     * 打印标准日志
     * @param msg       日志消息
     */
    public static trace(msg: any, color: string = "color:#000000;") {
        // 标记没有打开，不打印该日志
        if (!GameParams.Debug) {
            return;

        }
        var backLog = console.log || log;
        backLog.call(null, "%c%s%s", color, this.getDateString(), msg);
    }


    /**
     * 输出日志
     * @param tag       日志类型
     * @param msg       日志内容
     * @param color     日志文本颜色
     * @param describe  日志标题描述
     */
    public static print(msg: any, color: string = "color:#000000;") {
        // 标记没有打开，不打印该日志
        if (!GameParams.Debug) {
            return;
        }

        var backLog = console.log || log;
        backLog.call(null, "%c%s%s%s:%o", color, this.getDateString(), this.stack(5), msg);
    }

    private static stack(index: number): string {
        var e = new Error();
        var lines = e.stack!.split("\n");
        var result: Array<any> = [];
        lines.forEach((line) => {
            line = line.substring(7);
            var lineBreak = line.split(" ");
            if (lineBreak.length < 2) {
                result.push(lineBreak[0]);
            }
            else {
                result.push({ [lineBreak[0]]: lineBreak[1] });
            }
        });

        var list: string[] = [];
        var splitList: Array<string> = [];
        if (index < result.length - 1) {
            var value: string;
            for (var a in result[index]) {
                var splitList = a.split(".");

                if (splitList.length == 2) {
                    list = splitList.concat();
                }
                else {
                    value = result[index][a];
                    var start = value!.lastIndexOf("/");
                    var end = value!.lastIndexOf(".");
                    if (start > -1 && end > -1) {
                        var r = value!.substring(start + 1, end);
                        list.push(r);
                    }
                    else {
                        list.push(value);
                    }
                }
            }
        }

        if (list.length == 1) {
            return "[" + list[0] + ".ts]";
        }
        else if (list.length == 2) {
            return "[" + list[0] + ".ts->" + list[1] + "]";
        }
        return "";
    }

    private static getDateString(): string {
        let d = new Date();
        let str = d.getHours().toString();
        let timeStr = "";
        timeStr += (str.length == 1 ? "0" + str : str) + ":";
        str = d.getMinutes().toString();
        timeStr += (str.length == 1 ? "0" + str : str) + ":";
        str = d.getSeconds().toString();
        timeStr += (str.length == 1 ? "0" + str : str) + ":";
        str = d.getMilliseconds().toString();
        if (str.length == 1) str = "00" + str;
        if (str.length == 2) str = "0" + str;
        timeStr += str;

        timeStr = "[" + timeStr + "]";
        return timeStr;
    }
}